using Application.Core.Entity;
using Application.Service.Users;
using Application.Service.Permissions;
using Application.Web.Core.Services;

using BootstrapBlazor.Components;

using Microsoft.AspNetCore.Components;

using System.Collections.Generic;
using System.Threading.Tasks;
using Application.Service.LogCache;

namespace Application.Web.Entry.Pages
{
    public partial class Permissions
    {
		[Inject] public CheckPermission checkPermissionService { get; set; } = default!;
        [Inject] public PermissionService PermissionService { get; set; } = default!;

        [Inject] public NavigationManager Navigation { get; set; } = default!;

		private bool _hasPermission = false;
        private bool _isReadOnly = false;
        private bool _canAdd = false;
        private bool _canEdit = false;
        private bool _canDelete = false;

        protected override async Task OnInitializedAsync()
        {
            LogWrapper.Information("访问权限管理页面");
            
            // 检查页面访问权限
            _hasPermission = await checkPermissionService.CanAccessPageAsync("permission");
            
            if (!_hasPermission)
            {
				checkPermissionService.RedirectToNoPermission();
                return;
            }

            // 检查是否为只读权限
            _isReadOnly = await checkPermissionService.IsPageReadOnlyAsync("permission");

            // 检查操作权限
            _canAdd = await checkPermissionService.CanPerformOperationAsync("permission.create");
            _canEdit = await checkPermissionService.CanPerformOperationAsync("permission.modify");
            _canDelete = await checkPermissionService.CanPerformOperationAsync("permission.delete");

            await base.OnInitializedAsync();
        }

        private void GoToHome()
        {
            Navigation.NavigateTo("/");
        }

        // 表格相关属性和方法
        private IEnumerable<int> PageItemsSource { get; } = new int[] { 10, 20, 40 };

        private List<Permission> Items { get; set; } = new();

        private async Task<QueryData<Permission>> OnQueryAsync(QueryPageOptions options)
        {
			if (Items == null || Items.Count == 0)
			{
				Items = (await PermissionService.GetAllAsync()).ToList();
			}
			
			var items = Items.AsQueryable();
			
			// 应用过滤
			if (!string.IsNullOrEmpty(options.SearchText))
			{
				items = items.Where(p => 
					(p.Name != null && p.Name.Contains(options.SearchText)) ||
					(p.Code != null && p.Code.Contains(options.SearchText)) ||
					(p.Description != null && p.Description.Contains(options.SearchText)));
			}
			
			// 排序
			var isSorted = false;
			if (!string.IsNullOrEmpty(options.SortName))
			{
				items = items.Sort(options.SortName, options.SortOrder);
				isSorted = true;
			}

			var total = items.Count();
			var result = new QueryData<Permission>()
			{
				Items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList(),
				TotalCount = total,
				IsFiltered = !string.IsNullOrEmpty(options.SearchText),
				IsSorted = isSorted,
				IsSearch = true
			};
			
			return result;
        }

        private Task<bool> OnSaveAsync(Permission item, ItemChangedType changedType)
        {
            // 权限保存逻辑
            return Task.FromResult(true);
        }

        private Task<bool> OnDeleteAsync(IEnumerable<Permission> items)
        {
            // 权限删除逻辑
            return Task.FromResult(true);
        }
    }
}